package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.common.PageBean;
import com.itheima.common.R;
import com.itheima.dto.ClazzDto;
import com.itheima.dto.ClazzQueryDto;
import com.itheima.pojo.Clazz;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    ClazzService clazzService;

    //分页查询
    @GetMapping
    public R ClazzQuery(ClazzQueryDto dto) {
        PageBean pageBean = clazzService.ClazzQuery(dto);
        return R.ok(pageBean);
    }

    //查所有
    @GetMapping("/list")
    public R Queryall() {
        List<Clazz> clazz = clazzService.Queryall();
        return R.ok(clazz);
    }

    //添加
    @PostMapping
    @Log
    public R insert(@RequestBody ClazzDto clazz) {
        clazzService.insert(clazz);
        return R.ok();
    }

    /*删除*/
    @Log
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        clazzService.delete(id);
        return R.ok();
    }

    /*根据id查询*/
    @Log
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        Clazz clazz = clazzService.getById(id);
        return R.ok(clazz);
    }

    /*更新*/
    @Log
    @PutMapping
    public R update(@RequestBody ClazzDto clazz) {
        clazzService.update(clazz);
        return R.ok();
    }
}
